package com.wl156.activities;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.wl156.MyApp;
import com.wl156.R;
import com.wl156.activities.find.FindCarResult;
import com.wl156.activities.find.FindGoodMessageResult;
import com.wl156.activities.find.FindPlace;
import com.wl156.activities.order.MyOrder;
import com.wl156.model.Certificate;
import com.wl156.model.User;
import com.wl156.model.WebServiceClient;
import com.wl156.utils.Utils;

public class MyPage extends Activity implements OnClickListener {

	private TextView mCurrentPlace;
	private TextView mDriverName;
	private TextView mCreditScore;
	private TextView mFinishOrderCount;

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	String mUserType;

	@Override
	protected void onResume() {
		super.onResume();
		Button credit = (Button) findViewById(R.id.credit_detail);
		credit.setOnClickListener(this);
		Button myinfo = (Button) findViewById(R.id.myinfo);
		myinfo.setOnClickListener(this);
		Button order = (Button) findViewById(R.id.order);
		order.setOnClickListener(this);
		// final Button collectGood = (Button)
		// findViewById(R.id.my_collect_good);
		// collectGood.setOnClickListener(this);
		// Button collectLine = (Button) findViewById(R.id.my_collect_line);
		// collectLine.setOnClickListener(this);
		// Button collectGroupManager = (Button)
		// findViewById(R.id.collect_group_manage);
		// collectGroupManager.setOnClickListener(this);
		// Button findPlace = (Button) findViewById(R.id.find_place);
		// findPlace.setOnClickListener(this);

		mCurrentPlace = (TextView) findViewById(R.id.current_place);
		mDriverName = (TextView) findViewById(R.id.driver_name);
		mCreditScore = (TextView) findViewById(R.id.credit_score);
		mFinishOrderCount = (TextView) findViewById(R.id.finish_order_count);
		if (((MyApp) getApplication()).isLogined()) {
			User user = ((MyApp) getApplication()).getCurrentUser();
			if (user != null) {
				mUserType = user.type;
			}
			// FIXME: for test
			// mUserType = "D";

			new AsyncTask<Void, Void, Object>() {
				ProgressDialog mDialog;

				protected void onPreExecute() {
					mDialog = ProgressDialog.show(MyPage.this, "", "加载中");
				};

				@Override
				protected void onPostExecute(Object result) {
					// 02-25 21:32:35.101: D/(23726): result:
					// anyType{string=156物流;
					// string=95; string=20; string=5; string=广东广州; string=12;
					// string=3; string=1; string=0; }, type: SoapObject

					try {
						if (result instanceof SoapObject) {
							SoapObject so = (SoapObject) result;
							String name = Utils.propertyToShow(so
									.getProperty(0));
							String creditScroe = Utils.propertyToShow(so
									.getProperty(1));
							String finishedOrderCount = Utils.propertyToShow(so
									.getProperty(2));
							// String goingOrderCount =
							// Utils.propertyToShow(so.getProperty(3));
							String curentPlace = Utils.propertyToShow(so
									.getProperty(4));
							//
							// String collectedTrunkCount = so.getProperty(5)
							// .toString();
							// String collectedLineCount = so.getProperty(6)
							// .toString();
							// String isInternalUser = so.getProperty(7)
							// .toString();
							// String isSupportLogisticsCard = so.getProperty(8)
							// .toString();
							// String type = so.getPropertyAsString(9);

							// if (type.equals("C")) {
							// // 公司
							// collectGood.setText("我收藏的车");
							// } else {
							// collectGood.setText("我收藏的货讯");
							// // 司机
							// }

							mDriverName.setText(name);
							mCreditScore.setText(String.format("信用分数: %s",
									creditScroe));
							mCurrentPlace.setText(String.format("当前位置: %s",
									curentPlace));
							mFinishOrderCount.setText(String.format(
									"成功完成订单数量: %s", finishedOrderCount));
						} else {
							Toast.makeText(MyPage.this, "信息加载失败，请稍候重试",
									Toast.LENGTH_LONG).show();
						}
					} catch (Exception e) {
						Toast.makeText(MyPage.this, "信息加载失败，请稍候重试",
								Toast.LENGTH_LONG).show();
					}

					mDialog.dismiss();
				};

				@Override
				protected Object doInBackground(Void... params) {
					try {
						return mWebServiceClient
								.commonGetPersonalInfo(mCertificate);
					} catch (IOException e) {
						e.printStackTrace();
						return e;
					} catch (XmlPullParserException e) {
						e.printStackTrace();
						return e;
					}
					// try {
					// return mWebServiceClient.commonGetMyCurrentPlace(
					// mCertificate.userLoginId,
					// mCertificate.certifcateToken);
					// } catch (IOException e) {
					// e.printStackTrace();
					// return e;
					// } catch (XmlPullParserException e) {
					// e.printStackTrace();
					// return e;
					// }
				}
			}.execute();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Window win = getWindow();
		// win.requestFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.act_my_page);
		// win.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
		// R.drawable.ic_launcher);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

	}

	@Override
	public void onClick(View v) {
		Class<? extends Activity> startActivity = null;
		switch (v.getId()) {
		case R.id.credit_detail:
			startActivity = CreditSearch.class;
			break;
		case R.id.myinfo:
			startActivity = MyInfo.class;
			break;
		case R.id.order:
			startActivity = MyOrder.class;
			break;
		// case R.id.my_collect_good:
		// startActivity = mUserType.equals("C") ? MyCollectCar.class
		// : MyCollectGood.class;
		// break;
		// case R.id.my_collect_line:
		// startActivity = MyCollectLine.class;
		// break;
		// case R.id.collect_group_manage:
		// startActivity = MyCollectGroupManage.class;
		// break;
		// case R.id.find_place:
		// startActivity = FindPlace.class;
		// break;
		}
		Intent i = new Intent(this, startActivity);
		startActivity(i);
	}
}
